<?php
/**
 * 作用：公用模块，用于放置零碎，不能组成模块的功能函数及常量
 * 作者: shijy@soe-soe.com
 * 日期: 2016/5/11 16:09
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */


namespace Common\Model;


class UserModel extends BaseModel{
    const SEX_MALE   = 1; //性别男
    const SEX_FEMALE = 0; //性别女

    public static  function getSex(){
        return array(
            self::SEX_FEMALE=>'女',
            self::SEX_MALE=>'男',
        );
    }


    const RACE_YELLOW   =1;//
    const RACE_WHITE    =2;//
    const RACE_BLACK    =3;//
    public static  function getRace(){
        return array(
            self::RACE_YELLOW=>'黄种人',
            self::RACE_WHITE=>'白种人',
            self::RACE_BLACK=>'黑色人种',
        );
    }



    const USER_TYPE_ALL     = 0;
    const USER_TYPE_STAR    = 1;
    const USER_TYPE_NORMAL  = 2;

    public static function getUserType(){
        return array(
            self::USER_TYPE_ALL=>'全部',
            self::USER_TYPE_STAR=>'明星',
            self::USER_TYPE_NORMAL=>'素人',
        );
    }

    public static function getAuditStatus(){
        return array(
            PHOTO_AUDIT_ALL     =>'全部',
            PHOTO_AUDIT_UNCHECK =>'待审',
            PHOTO_AUDIT_PASS    =>'审核通过',
            PHOTO_AUDIT_REVIEW  =>'待审核',//AI不能判断
            PHOTO_AUDIT_UNPASS  =>'审核不通过'
        );
    }


    /**
     * 根据过关数据获得对应称号
     * @param $score
     * @return string
     */
    public static function getTitle($score){
        if ($score<5){//5
            return '凡夫肉眼';
        }elseif($score<15){//10
            return '眼光独到';
        }elseif($score<30){//15
            return '洞若观火';
        }elseif($score<50){//20
            return '目光如炬';
        }elseif($score<75){//25
            return '别具慧眼';
        }elseif($score<105){//30
            return '慧眼识珠';
        }elseif($score<140){//35
            return '伯乐再世';
        }elseif($score<180){//40
            return '火眼金睛';
        }elseif($score<225){//45
            return '洞若神明';
        }else{
            return '上帝之眼';
        }
    }


    private static $shareInstance=null;
    public static function getShareInstance(){
        if(!self::$shareInstance){
            self::$shareInstance=M('u_share',null,DB_MAIN_CFG);
        }
        return self::$shareInstance;
    }

    private static $cashInstance=null;
    public static function getCashInstance(){
        if(!self::$cashInstance){
            self::$cashInstance=M('cash_log',null,DB_MAIN_CFG);
        }
        return self::$cashInstance;
    }


//素人
    private static $userInstance=null;
    public static function getUserInstance(){
        if(!self::$userInstance){
            self::$userInstance=M('u_user',null,DB_MAIN_CFG);
        }
        return self::$userInstance;
    }


//明星
    private static $starInstance=null;
    public static function getStarInstance(){
        if(!self::$starInstance){
            self::$starInstance=M('s_star',null,DB_MAIN_CFG);
        }
        return self::$starInstance;
    }

    public static function getMaxStarId(){
        return self::getStarInstance()->max('uid');
    }


    public static function getUserDetail($uid){
        return self::getUserInstance()->where('uid='.$uid)->find();
    }



    /**
     * 金额转移
     * @param $money
     * @param $toUid
     * @param $opt
     * @param $fromUid
     */
    public static function addMoney($moneyFen,$toUid,$opt,$fromUid=0){
        self::getUserInstance()->where("uid={$toUid}")->setInc('depositMoney',$moneyFen);
        $moneyYuan=$moneyFen/100;
        $otherNickname=$fromUid>0?self::getNickname($fromUid):'';

        switch ($opt){
            case GameModel::OPT_BET_RETURN:
                $msg="对垒'{$otherNickname}'获胜，退回金额：{$moneyYuan}元";
                break;
            case GameModel::OPT_BET_WIN:
                $msg="对垒'{$otherNickname}'获胜，赚得奖励：{$moneyYuan}元";
                break;
            case GameModel::OPT_BET_LOSE:
                $msg="'{$otherNickname}'输给您：{$moneyYuan}元";
                break;
            case GameModel::OPT_PRIZE:
                $msg="找对'{$otherNickname}'，赚得奖励：{$moneyYuan}元";
                break;
            case GameModel::OPT_OVERTIME:
                $msg="游戏超过时效，退回：{$moneyYuan}元";
                break;
        }
        GameModel::addLog($toUid,$msg);
    }

    public static function getNickname($uid){
        return self::getUserInstance()->where("uid={$uid}")->getField('nickname');
    }

    public static function login($openId,$nickname,$headPic,$sex){
        $uinfo=self::getUserInstance()->where("openid='{$openId}'")->find();
        if($uinfo){
            $uinfo['rank']=self::getUserInstance()->where("historyScore>{$uinfo['historyScore']} and uid!={$uinfo['uid']}")->count()+1;
            $uinfo['title']=self::getTitle($uinfo['historyScore']);
        }else{
            $uid=self::getUserInstance()->add(array(
                'nickname'  => $nickname,
                'openId'    => $openId,
                'headPic'   => $headPic,
                'sex'       => $sex,
                'point'     => 2000, //初始新用户送3000
                'addTime'   => time()
            ));
            $uinfo=self::getUserInstance()->where("uid='{$uid}'")->find();
            $uinfo['rank']='999+';
            $uinfo['title']=self::getTitle(0);
        }
        session('uid',$uinfo['uid']);
        $uinfo['feeScale']=FEE_SCALE;
        $uinfo['timeLimit']=PLAY_TIME_LIMIT;
        return $uinfo;
    }

    /**
     * 新建素人游戏
     * @param $uid
     * @param $albumStr
     * @param $target
     * @param string $type
     * @param string $extra
     * @param int $gid
     * @return int|mixed
     */
    public static function editGame($uid,$albumStr,$target,$type,$extra='',$gid=0){
        $pics=str_replace(',',"','",$albumStr);
        $audit=M('photo_lib',null,DB_MAIN_CFG)->where("path in ('{$pics}')")->max('audit');
        if (!$gid){
            $newData=array(
                'uid'=>$uid,
                'target'=>$target,
                'album'=>$albumStr,
                'audit'=>$audit,
                'addTime'=>time(),
                'type'=>$type
            );
            if ($type=='group'){
                $newData['extra']=$extra;
            }
            $gid=GameModel::getGameInstance()->add($newData);
        }else{
            $newData=array(
                'target'=>$target,
                'album'=>$albumStr,
                'audit'=>$audit
            );
            if ($type=='group'){
                $newData['extra']=$extra;
            }
            GameModel::getGameInstance()->where("gid={$gid} and uid={$uid}")->save($newData);
        }
        switch ($audit){
            case PHOTO_AUDIT_PASS:
                $status=0;
                $msg="图片审核通过";
                break;
            case PHOTO_AUDIT_REVIEW:
                $status=1;
                $msg="有图片需要人工审核，\r\n请待审核通过后继续操作";
                break;
            case PHOTO_AUDIT_UNPASS:
                $status=2;
                $msg="由于图片中包含色情信息，游戏不可创建";
                break;
        }
        return array('status'=>$status,'gid'=>$gid,'audit'=>$audit,'msg'=>$msg);
    }
}